package com.herui.iot.ui.evaluation;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import butterknife.BindView;
import butterknife.OnClick;

import com.alibaba.fastjson.JSON;
import com.herui.sdyu_lib.adapter.recycle_view.base.ViewHolder;
import com.herui.sdyu_lib.base.FragmentDirectionAnim;
import com.herui.sdyu_lib.utils.ToastUtil;
import com.herui.sport_care.R;
import com.herui.sport_care.data.eval.EvalInfo;
import com.herui.sport_care.ui.BaseListSportFragment;

import java.util.List;

/**
 * 新增评估
 */
public class EvalDayFragment extends BaseListSportFragment<EvalInfo> {

    @BindView(R.id.id_recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
//    @BindView(R.id.v_eval_line_cut)
//    View evalLineCut;
//    @BindView(R.id.tv_eval_time)
//    TextView evalTime;

    private EvalPresenter evalPresenter;

    public EvalDayFragment() {
        evalPresenter = new EvalPresenter(view);
    }

    public static EvalDayFragment newInstance() {
        EvalDayFragment fragment = new EvalDayFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayout() {
        return R.layout.fragment_eval_day;
    }

    @Override
    public void initParam() {
        initList(recyclerView, R.layout.item_home_list, false, null);
        enablePullRefresh(swipeRefreshLayout);
    }

    @Override
    public void onResume() {
        super.onResume();

        refresh();
    }

    @OnClick({R.id.view_add})
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.view_add:
                baseActivity.jump(EvalAddActivity.class);
                break;
            default:
                break;
        }
    }

    EvalPresenter.View view = new EvalPresenter.View() {
        @Override
        public void onDayEvalInfo(List<EvalInfo> result) {
            addData(result);
        }
        @Override
        public void error(String msg) {
            baseActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ToastUtil.showMessage(msg);
                    loadMoreFail();
                }
            });
        }
    };

    @Override
    protected void convert(ViewHolder holder, EvalInfo evalInfo, int position) {
        holder.setText(R.id.tv_name, evalInfo.getName());
        holder.setText(R.id.tv_agreement,"协议-" + evalInfo.getAgreementName());
        holder.setText(R.id.tv_time,evalInfo.getEvaluationDate() + "   " + evalInfo.getEvaluationStartTime());
        holder.setVisible(R.id.v_eval_line_cut,false);
        holder.setVisible(R.id.tv_eval_time,false);

        holder.setOnClickListener(R.id.btn_item_eval, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(baseActivity, EvalActivity.class);
                intent.putExtra("evalInfo", JSON.toJSONString(evalInfo));
                baseActivity.jump(intent);
            }
        });

        switch (evalInfo.getEvaluationStatus()){
            case NOT_STARTED:
                holder.setText(R.id.tv_eval_status,"未开始");
                holder.setTextColor(R.id.tv_eval_status,0xff7B7B7B);
                holder.setBackgroundRes(R.id.tv_eval_status,R.drawable.home_text_bg_1);
                holder.setBackgroundRes(R.id.view_item_bg,R.drawable.login_btn_bg_2);
                holder.setText(R.id.btn_item_eval,"开始评估");
                break;
            case IN_PROGRESS:
                holder.setText(R.id.tv_eval_status,"进行中");
                holder.setTextColor(R.id.tv_eval_status,0xff63E6BE);
                holder.setBackgroundRes(R.id.tv_eval_status,R.drawable.home_text_bg_2);
                holder.setBackgroundRes(R.id.view_item_bg,R.drawable.login_btn_bg_1);
                holder.setText(R.id.btn_item_eval,"继续评估");
                break;
            case FINISHED:
                holder.setText(R.id.tv_eval_status,"已完成");
                holder.setTextColor(R.id.tv_eval_status,0xff63E6BE);
                holder.setBackgroundRes(R.id.tv_eval_status,R.drawable.home_text_bg_2);
                holder.setBackgroundRes(R.id.view_item_bg,R.drawable.login_btn_bg_1);
                holder.setText(R.id.btn_item_eval,"查看详情");
                break;
            default:
                break;
        }
    }

    @Override
    protected void doRefresh() {
        evalPresenter.getEvalDay();
    }
}